package org.test2;

import org.test1.Animal;

import java.util.Arrays;
import java.util.Comparator;

public class Employee implements Comparable<Employee> {
    public int id;
    public String name;

    public int age;

    public Employee(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    @Override
    public int compareTo(Employee o) {
        if (this.id > o.id) {
            return 1;
        } else if (this.id == o.id) {
            return 0;
        } else {
            return -1;
        }
    }

    @Override
    public String toString() {
        return "{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public static void main(String[] args) {
        Employee[] employees = new Employee[]{new Employee(10, "张三", 20),
                new Employee(11, "王五", 25),
                new Employee(5, "李四", 38)};
        //1.使用comparable接口的compare方法
        Arrays.sort(employees);
        System.out.println(Arrays.toString(employees));

        //2.使用比较器
        Arrays.sort(employees, new AgeComparator());
        System.out.println(Arrays.toString(employees));
    }
}

class AgeComparator implements Comparator<Employee> {
    @Override
    public int compare(Employee o1, Employee o2) {
        return o1.age - o2.age;
    }
}
